package com.yueya.common.redis;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

public class JedisKit {

	private static JedisPool pool;

	public static void init(JedisPool pool) {
		JedisKit.pool = pool;
	}

	/**
	 * 获取Jedis实例
	 * 
	 * @return
	 */
	public synchronized static Jedis getJedis() {
		try {
			if (pool != null) {
				Jedis resource = pool.getResource();
				return resource;
			} else {
				return null;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	public synchronized static Jedis getJedis(int db) {
		try {
			if (pool != null) {
				Jedis resource = pool.getResource();
				resource.select(db);
				return resource;
			} else {
				return null;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * 释放jedis资源
	 * 
	 * @param jedis
	 */
	public static void returnResource(final Jedis jedis) {
		if (jedis != null) {
			pool.returnResource(jedis);
		}
	}

}